<%--
  Created by IntelliJ IDEA.
  User:lixiangling
  Date: 2018-09-06
  Time: 18:34:15
  desc: MQ消费失败信息的展示界面
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
	<%@ include file="../../include/head.jsp"%>
	<%@ include file="../../include/layui.jsp"%>
	<title>MQ消费失败信息列表</title>
</head>
<body layadmin-themealias="default">
<div class="layui-fluid">
	<div class="layui-card">
		<div class="layui-form layui-card-header layuiadmin-card-header-auto">
			<!-- start 搜索框 -->
			<div class="layui-collapse">
				<div class="layui-colla-item">
					<h2 class="layui-colla-title">搜索操作</h2>
					<div class="layui-colla-content layui-show">
						<!-- 搜索相关表单及控件 参照`表单`样式 -->
						<form class="layui-form layui-form-pane" id="search-form">
							<!-- start 行信息 -->
							<div class="layui-form-item" style="margin-bottom: 0;">
								<!-- start 单个元素项 -->
								<div class="layui-inline">
									<label class="layui-form-label">主题名称</label>
									<div class="layui-input-inline">
										<select name="topicName" >

											<option value="ACTIVITY_TASK_AWARD">完成任务奖励</option>
											<!--
											<option value="OAUTH_LOGIN">登录</option>
											<option value="ACTIVITY_TASK_GAME">火树接口游戏任务</option>
											<option value="SEND_SMS">发送短信</option>
											<option value="SEND_VOICE">发送语音</option> -->
										</select>
									</div>
								</div>
								<!-- end 单个元素项 -->
								<!-- start 单个元素项 -->
								<div class="layui-inline">
									<label class="layui-form-label">处理状态</label>
									<div class="layui-input-inline">
										<select name="execStatus" >
											<option value="-1">全部</option>
											<option value="0">待处理</option>
											<option value="1">处理中</option>
											<option value="2">取消处理</option>
											<option value="3">处理成功</option>
											<option value="4">处理失败</option>
										</select>
									</div>
								</div>
								<!-- end 单个元素项 -->
								<!-- start 单个元素项 -->
								<div class="layui-inline">
									<label class="layui-form-label">全局唯一标识</label>
									<div class="layui-input-inline">
										<input type="text"  id="globalId" name="globalId" lay-verify="title" autocomplete="off" placeholder="请输入全局唯一标识"  class="layui-input">
									</div>
								</div>
								<!-- end 单个元素项 -->
								<!-- start 单个元素项 -->
								<div class="layui-inline">
									<button type="button" class="layui-btn layui-btn-radius btn-min-width" id="search-btn">
										<i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
									</button>
									<button type="reset" class="layui-btn layui-btn-radius layui-btn-primary btn-min-width">清空</button>
								</div>
							</div>
						</form>
					</div>
				</div>
			</div>
			<!-- end 搜索框 -->
		</div>
		<div class="layui-card-body" >
			<!-- start 表格内容 -->
			<div>
				<!-- start 顶部按钮 -->
				<div class="layui-btn-group btn-group-bottom" btn-listener>
					<shiro:hasPermission name="maint:mqFailInfo_compensate">
						<button class="layui-btn layui-btn-orange layui-btn-sm" lay-event="compensate" not-null="1">
							<i class="layui-icon">&#xe642;</i>补偿
						</button>
					</shiro:hasPermission>
					<shiro:hasPermission name="maint:mqFailInfo_compensateBatch">
						<button class="layui-btn layui-btn-blue layui-btn-sm" lay-event="compensateBatch"  title="补偿当前标题的所有消息">
							<i class="layui-icon">&#xe6b1;</i>全部补偿
						</button>
					</shiro:hasPermission>
					<shiro:hasPermission name="maint:mqFailInfo_cancel">
						<button class="layui-btn layui-btn-red layui-btn-sm" lay-event="cancel" not-null="1">
							<i class="layui-icon">&#x1007;</i>取消
						</button>
					</shiro:hasPermission>
					<shiro:hasPermission name="maint:mqFailInfo_export">
						<button class="layui-btn layui-btn-green layui-btn-sm" lay-event="exportData">
							<i class="layui-icon">&#xe621;</i>导出
						</button>
					</shiro:hasPermission>
				</div>
				<!-- end 顶部按钮 -->
				<!-- start 表头按钮 -->
				<!-- end 表头按钮 -->
				<table class="layui-hide" id="data-table" lay-filter="data-table"></table>
			</div>

			<!-- 行内操作按钮 -->
			<script type="text/html" id="toolBar">
				<a class="layui-btn-xs" title="查看" style="cursor:pointer" lay-event="detail"><i class="layui-icon" style="font-size: 40px; color: #1E9FFF;font-weight: bold">&#xe60a;</i></a>
			</script>
			<!-- end 表格内容 -->
		</div>
	</div>
</div>
<script type="text/html" id="execStatusTpl">
	{{#  if(d.execStatus == 0){ }}
	<span class="layui-btn layui-btn-primary layui-btn-xs" style="width: 60px">待处理</span>
	{{#  } else if(d.execStatus == 1){ }}
	<span class="layui-btn layui-btn-orange layui-btn-xs" style="width: 60px">处理中</span>
	{{#  } else if(d.execStatus == 2){ }}
	<span class="layui-btn layui-btn-blue layui-btn-xs" style="width: 60px">取消补偿</span>
	{{#  } else if(d.execStatus == 3){ }}
	<span class="layui-btn layui-btn-green layui-btn-xs" style="width: 60px">处理成功</span>
	{{#  } else if(d.execStatus == 4){ }}
	<span class="layui-btn layui-btn-red layui-btn-xs" style="width: 60px">处理失败</span>
	{{#  }  }}
</script>
<script type="text/javascript">

    layui.use(['index', 'element', 'layer', 'table','laydate'], function(){
        var table = layui.table;

        //方法级渲染
        table.render({
            elem: '#data-table',
            url:  '${ctx}/maint/mqFailInfo/list',
            where: getParamJson('#search-form'),
            cols: [[

                {field:'mqFailId', title: 'MQ消费失败数据编号', checkbox: true}
                ,{field:'topicName', title: '主题名称',width:120}
                ,{field:'mqMessage', title: '消息',width:120}
                ,{field:'execStatus', title: '处理状态',width:120,templet:execStatusTpl}
                ,{field:'reason', title: '原因',width:120}
                ,{field:'createTime', title: '创建时间',width:120}
                ,{field:'modifyTime', title: '修改时间',width:120}
                ,{field:'dealInfo', title: '处理信息',width:120}
                ,{field:'failInfo', title: '失败信息',width:120}
                ,{field:'globalId', title: '全局唯一标识',width:120}
                ,{title: '操作', width: 90, toolbar: '#toolBar',fixed:'right'}
            ]]
            ,page: true
        });
        // 搜索按钮点事件
        $('#search-btn').on('click', function(){
            table.reload('data-table', {
                where: getParamJson('#search-form'),
                page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
        });

        // 校验初始化
        // 校验初始化
        var form = layui.form;
        form.verify({
			globalId: function(value, item){
                if(!/^.{0,9223372036854775807}$/.test(value)){
                    return '全局唯一标识不能超过9223372036854775807'
                }
            }
        });

        // 行操作事件定义
        var tableEvent = {
            detail: function(obj){
                var data = obj.data; //获得当前行数据
                var tr = obj.tr; //获得当前行 tr 的DOM对象
                openTabsPage('${ctx}/maint/mqFailInfo/detail?id=' + data.mqFailId, '查看MQ消费失败信息 - ' + data.mqFailId);
            }
        };

        // 其他操作按钮事件定义
        var otherBtnEvent = {
            getPrimaryKeyParams: function(data){
                var params = '';
                $.each(data, function(i, item){
                    if(i !== 0) params += '&';
                    params += 'id=' + item.mqFailId;
                });
                return params
            },
			checkInit: function (data) {
                var isInit=true;
                $.each(data, function(i, item){
                    if(item.execStatus!=0){
                        isInit = false;
                        parent.layer.msg('请选择待处理的操作的记录');
                        return;
                    }
                });
                return isInit;
            },
            compensate: function(data, length, isAll){
                if(!otherBtnEvent.checkInit(data)){
                    return;
				}
                var params = otherBtnEvent.getPrimaryKeyParams(data);
                parent.layer.confirm('真的补偿选中的信息么', function(index){
                    parent.layer.close(index);
                    //向服务端发送删除指令
                    $.post('${ctx}/maint/mqFailInfo/compensate', params,function(result){
                        if(result.code == 0){
                            parent.layer.alert('操作成功', {
                                yes: function(index){
                                    parent.layer.close(index);
                                    // 更新表格
                                    table.reload('data-table');
                                }
                            });
                        }else{
                            parent.layer.msg(result.msg || '操作失败');
                        }
                    }, 'json');
                });
            },
            cancel: function(data, length, isAll){//取消
                if(!otherBtnEvent.checkInit(data)){
                    return;
                }
                var params = otherBtnEvent.getPrimaryKeyParams(data);
                parent.layer.prompt({
                    formType: 2,
                    value: '',
                    title: '请输入取消补偿原因',
                    area: ['300px', '150px'] //自定义文本域宽高
                }, function(value, index, elem){
                    parent.layer.close(index);
                    var aparams = "reason="+value;
                    //向服务端发送删除指令
                    $.post('${ctx}/maint/mqFailInfo/cancel', aparams,function(result){
                        if(result.code == 0){
                            parent.layer.alert('操作成功', {
                                yes: function(index){
                                    parent.layer.close(index);
                                    // 更新表格
                                    table.reload('data-table');
                                }
                            });
                        }else{
                            parent.layer.msg(result.msg || '操作失败');
                        }
                    }, 'json');
                });
            },
            exportData: function(data, length, isAll){ //导出
                // 数量限制
                var params = $.param({
                    page: 1,
                    limit: 1000
                }, true);
                // 选中的数据
                var primaryKeys = otherBtnEvent.getPrimaryKeyParams(data);
                // 搜索参数

                // 聚合
                params = params + '&' + primaryKeys;
                window.open('${ctx}/maint/mqFailInfo/export?' + params);
            },
            compensateBatch: function(data, length, isAll){ // 全部补偿
                parent.layer.confirm('确定要全部补偿么', function(index){
                    parent.layer.close(index);
                    //向服务端发送删除指令
                    $.post('${ctx}/maint/mqFailInfo/compensateBatch', params,function(result){
                        if(result.code == 0){
                            parent.layer.alert('操作成功', {
                                yes: function(index){
                                    parent.layer.close(index);
                                    // 更新表格
                                    table.reload('data-table');
                                }
                            });
                        }else{
                            parent.layer.msg(result.msg || '操作失败');
                        }
                    }, 'json');
                });
            }
        };

        // 初始化监听事件
        initTableEvnet(table, 'data-table', tableEvent, otherBtnEvent);
    });

</script>
</body>
</html>
